<?php
if(!defined('IN_ORA')) {
	exit('Access Denied');
}
$GLOBALS['__msg_tpl__'] = "admin/msg.html";
session_start();
if(!isset($_SESSION["admin"])){
	exit('Access Denied');
}
$act = $GLOBALS['act'];
$GLOBALS['tpl']->assign('act',$act);
import("lib.cat");
////////////////
if($act == 'list'){
	$GLOBALS['tpl']->assign('cats',cat_lib::getSubChild(0));
}
//新增
else if($act == 'add'|$act == 'modify'){
	$pid = is_numeric($GLOBALS['_param']['pid'])?$GLOBALS['_param']['pid']:0;
	$id = is_numeric($GLOBALS['_param']['id'])?$GLOBALS['_param']['id']:0;
	if($id){
		$cat = $GLOBALS['db']->getOne("select * from ".tn('cat')." where Id = $id");
		$pid = $cat['pid'];
	}
	$GLOBALS['tpl']->assign('cat',$cat);
	$GLOBALS['tpl']->assign('pid',$pid);
	$GLOBALS['tpl']->assign('cats',cat_lib::getSubChild(0,0));
}
//保存
else if($act == 'save'){
	$cat_id = is_numeric($_POST['cat_id'])?$_POST['cat_id']:0;
	$cat_name = $_POST['cat_name'];
	$pid = is_numeric($_POST['pid'])?$_POST['pid']:0;
	$pic = $_POST['pic'];
	if(is_file(UPLOAD_PATH.$pic)){
		@io::remove(UPLOAD_PATH.$pic);
		@io::remove(UPLOAD_PATH.timg($pic));
	}
	$cat_type = $_POST['cat_type']==1?1:0;
	if($cat_name == ''){
		showMsg('请输入类别名称!',$_SERVER['HTTP_REFERER'],1);
	}
	import("lib.upload");
	$up = new Upload("jpg|gif|png");
	if(!$up->errInfo){
		$pic = $up->files[0]["urlPath"];
		import("lib.image");
		image::thumb(UPLOAD_PATH.$pic,'','',100,80);
	}
	if($pid > 0){
		$cat_path = $GLOBALS['db']->getOne("select * from ".tn('cat')." where id = $pid");
		$cat_path = $cat_path['path'];
		$curr_path = $cat_path."-".$pid;
	}else{
		$curr_path = '0';
	}
	$sql = "insert into ".tn('cat')."(cat_name,cat_type,pid,pic,path) values('$cat_name',$cat_type,$pid,'$pic','$curr_path')";
	if($cat_id > 0){
		$sql = "update ".tn('cat')." set path='$curr_path',cat_name = '$cat_name' , cat_type = $cat_type , pid = $pid , pic = '$pic' where Id = $cat_id";
	}
	if($GLOBALS['db']->execute($sql)){
		showMsg('操作成功!',parse_uri('admin_cat-list'),1);
	}else{
		showMsg('操作失败!',parse_uri('admin_cat-list'),1);
	}
}
//删除类别
else if($act == 'del'){
	$id = $_GET['id'];
	if(!is_numeric($id)){
		showMsg("非法参数!");
	}
	$GLOBALS['db']->execute("delete from ".tn('cat')." where pid = $id or id = $id");
	showMsg('操作成功!',parse_uri('admin_cat-list'),1);
}
$GLOBALS['tpl']->display("admin/cat.html");


//------------private function ----------------//


?>